package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.adapter.ChangYongYuAdapter;
import com.mission.schedule.adapter.DaoJiShiTiXingAdapter;
import com.mission.schedule.bean.CYTXBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.utils.LineGridView;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 常用提醒对话框
 * Created by liwei on 2017/4/18.
 */

public class ReminderEditDialog extends Dialog {


    CallBack callBack;


    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    LinearLayout bianji;

    double timenumber = 5;


    public ReminderEditDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager,double num) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_changyong_edit, null);
        setContentView(view);

        timenumber = num;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        bianji = (LinearLayout) view.findViewById(R.id.bianji);

        final TextView number = (TextView) view.findViewById(R.id.number);
        number.setText((int)timenumber+"");

        Button aone = (Button) view.findViewById(R.id.aone);
        Button jone = (Button) view.findViewById(R.id.jone);
        Button aten = (Button) view.findViewById(R.id.aten);
        Button jten = (Button) view.findViewById(R.id.jten);
        aone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timenumber++;
                if (timenumber < 600){
                    number.setText((int)timenumber+"");
                }else {
                    timenumber = Integer.valueOf(number.getText().toString());
                    Toast.makeText(context,"最大值不能大于600",Toast.LENGTH_SHORT).show();
                }
            }
        });

        jone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timenumber--;
                if (timenumber < 5){
                    timenumber = Integer.valueOf(number.getText().toString());
                    Toast.makeText(context,"最小值不能低于5",Toast.LENGTH_SHORT).show();
                }else {
                    number.setText((int)timenumber+"");
                }
            }
        });

        aten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timenumber+=10;
                if (timenumber < 600){
                    number.setText((int)timenumber+"");
                }else {
                    timenumber = Integer.valueOf(number.getText().toString());
                    Toast.makeText(context,"最大值不能大于600",Toast.LENGTH_SHORT).show();
                }
            }
        });

        jten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timenumber-=10;
                if (timenumber < 5){
                    timenumber = Integer.valueOf(number.getText().toString());
                    Toast.makeText(context,"最小值不能低于5",Toast.LENGTH_SHORT).show();
                }else {
                    number.setText((int)timenumber+"");
                }
            }
        });

        view.findViewById(R.id.queren).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timenumber > 60 && (timenumber % 60 == 0 || timenumber % 30 ==0)){
                    callBack.reminderEditTime(timenumber / 60.0,2);
                }else {
                    callBack.reminderEditTime(timenumber,1);
                }

                dismiss();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void reminderEditTime(double number,int type);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
